// vue.config.js
const path = require('path');
const webpack = require('webpack');
const CompressWebpackPlugin = require('compression-webpack-plugin');
const productionGzipExtensions = ['js','css'];

//配置环境接口地址
const modeUrlObj = {
    // 生产环境
    production: 'http://120.78.5.31:8080/',
    // 开发环境
    // development: 'http://120.78.5.31:8080/',
    development: 'http://127.0.0.1:7878/', //本地环境
    // 测试环境
    test: 'http://xxxx:9091/test/',
}
module.exports = {
    // externals: {
    //     moment: 'moment'
    // },
    //基本路径
    //全局引入scss
    css: {
        loaderOptions: {
            // pass options to sass-loader
            sass: {
                // @/ is an alias to src/
                // so this assumes you have a file named `src/variables.scss`
                prependData: `
                @import "~@/assets/scss/variable.scss";
                @import "~@/assets/scss/mixins.scss";
                `
            }
        }
    },
    //接口代理
    devServer: {
        proxy: {
            '/api': {
                target: modeUrlObj[process.env.NODE_ENV],
                changeOrigin: true,
                pathRewrite: {
                    '/api': '/xdeer_mall'
                },
                secure: false
            }
        }
    },
    runtimeCompiler: true,

    // 打包优化
    productionSourceMap: false,
    // productionGzip: true
    configureWebpack:{
        plugins: [
            // 解决moment.js不必要引入
            new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /zh-cn/),
            //压缩打包文件
            new CompressWebpackPlugin({
                algorithm: 'gzip',
                test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
                threshold: 10240,
                minRatio: 0.8
            }),
            // chunk配置
            new webpack.optimize.LimitChunkCountPlugin({
                maxChunks: 5,
                minChunkSize: 100
            })
        ]
    }
}